package org.qs.webservice.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.qs.webservice.dao.content.ConBookMapper;
import org.qs.webservice.dao.content.ConBookStatMapper;
import org.qs.webservice.dao.user.UsBookShelfMapper;
import org.qs.webservice.domain.ConBook;
import org.qs.webservice.domain.ConBookStat;
import org.qs.webservice.domain.UsBookShelf;
import org.qs.webservice.service.BookShelfService;
import org.qs.webservice.service.IreadMemcacheService;
import org.qs.webservice.service.UpdateDataService;
import org.qs.webservice.util.DateUtil;
import org.qs.webservice.util.IReadUtils;
import org.qs.webservice.util.IdWorker;
import org.qs.webservice.util.StringUtils;
import org.qs.webservice.vo.BookReview;
import org.qs.webservice.vo.Content;
import org.qs.webservice.vo.PageData;
import org.springframework.stereotype.Service;

@Service
public class BookShelfServiceImpl implements BookShelfService {
	@Resource
	private UsBookShelfMapper usBookShelfMapper;
	@Resource
	private ConBookMapper conBookMapper;
	@Resource
	private ConBookStatMapper conBookStatMapper;
	@Resource
	private IreadMemcacheService ireadMemcacheService;
	@Resource
	private UpdateDataService updateDataService;
	@Override
	public String updateShelf(int book_id, String token,
			String device_identifier) throws Exception {
		UsBookShelf usBookShelf =  new UsBookShelf();
		Map<String, Object> tokenMap = new HashMap<String, Object>();
		usBookShelf.setBook_id(book_id);
		String user_id = IReadUtils.getUserIdByToken(token, ireadMemcacheService, device_identifier);
		if(StringUtils.isNullOrEmpty(user_id)){
			tokenMap.put("code", 403);
			tokenMap.put("reason", "用户未登录");
			return IReadUtils.beanToJson(tokenMap);
		}
		
		ConBook conBook = conBookMapper.getBookById(book_id);
		usBookShelf.setUser_id(Long.valueOf(user_id));
		if(conBook==null){
			tokenMap.put("code", 403);
			tokenMap.put("reason", "书籍不存在");
			return IReadUtils.beanToJson(tokenMap);
		}
		int ret = usBookShelfMapper.updateShelfStatus(usBookShelf);
		if(ret==1){
			ConBookStat conBookStat = conBookStatMapper.getBookStatById(book_id);
			if(!updateDataService.updateConBookStat(conBookStat, book_id, 1,1,null,0)){
				throw new Exception("打赏异常");
			}
		}
	
		tokenMap.put("code", 200);
		tokenMap.put("content", "操作成功");
		return IReadUtils.beanToJson(tokenMap);
		
	}
	@Override
	public String deleteShelf(List<String> mark_ids,String token,String device_identifier,String mark_id_str,String book_id) {
		Map<String, Object> tokenMap = new HashMap<String, Object>();
		
		String user_id = IReadUtils.getUserIdByToken(token, ireadMemcacheService, device_identifier);
		if(StringUtils.isNullOrEmpty(user_id)){
			tokenMap.put("code", 403);
			tokenMap.put("reason", "用户未登录");
			return IReadUtils.beanToJson(tokenMap);
		}
		if(!StringUtils.isNullOrEmpty(mark_id_str)){
			String[] mark_ids_arr = mark_id_str.split(",");
			for(String mark_id:mark_ids_arr){
				usBookShelfMapper.deleteShelf(Long.valueOf(mark_id),Long.valueOf(user_id));
			}
		}
		for(String mark_id:mark_ids){
			usBookShelfMapper.deleteShelf(Long.valueOf(mark_id),Long.valueOf(user_id));
		}
		if(!StringUtils.isNullOrEmpty(book_id)){
			usBookShelfMapper.deleteShelfByBookId(Long.valueOf(book_id),Long.valueOf(user_id));
		}
		tokenMap.put("code", 200);
		tokenMap.put("content", "操作成功");
		return IReadUtils.beanToJson(tokenMap);
	}
	@Override
	public String listShelf(int pages, int contents, String token,
			String device_identifier) {
		Map<String, Object> tokenMap = new HashMap<String, Object>();
		String user_id = IReadUtils.getUserIdByToken(token, ireadMemcacheService, device_identifier);
		if(StringUtils.isNullOrEmpty(user_id)){
			tokenMap.put("code", 403);
			tokenMap.put("reason", "用户未登录");
			return IReadUtils.beanToJson(tokenMap);
		}
		if(pages==0){
			pages=1;
		}
		if(contents==0){
			contents=20;
		}
		int index=(pages-1)*contents;
		List<UsBookShelf> list = usBookShelfMapper.getShelf(index, contents, Long.valueOf(user_id));
		List<Content>books = new ArrayList<Content>();
		for(UsBookShelf usBookShelf :list){
			Content bookShelf = new Content();
			bookShelf.setMark_id(usBookShelf.getMark_id()+"");
			bookShelf.setContent_id(usBookShelf.getBook_id());
			bookShelf.setContent_name(usBookShelf.getBook_name());
			if(usBookShelf.getChap_id()==0){
				bookShelf.setChapter_id(usBookShelf.getFirst_chap_id());
			}else
				bookShelf.setChapter_id(usBookShelf.getChap_id());
			bookShelf.setChapter_name(usBookShelf.getChap_name());
			bookShelf.setAuthor_name(usBookShelf.getAuthor_name());
			bookShelf.setAuthor_id(usBookShelf.getAuthor_id()+"");
			bookShelf.setChapter_offset(usBookShelf.getChapter_offset());
			bookShelf.setMark_time(DateUtil.getString(usBookShelf.getModify_time(), "yyyy-MM-dd HH:mm:ss"));
			bookShelf.setImage_url(usBookShelf.getBook_cover());
			books.add(bookShelf);
		}
		
		PageData<Content> pageData = new PageData<Content>();
		pageData.setCode(200);
		pageData.setPageNumber(pages);
		pageData.setPageSize(contents);
		pageData.setContent(books);
		pageData.setTotal(usBookShelfMapper.getShelfNum(Long.valueOf(user_id)));
		return IReadUtils.beanToJson(pageData);
	}
}
